<?php
/*========================================= JooSam =========================================/
/																							/
/	Extention:		JooSam																	/
/	Created by:		Broadcastweb.eu - Paules												/
/	Site:			http://www.broadcastweb.eu												/
/	Email:			support@broadcastweb.eu													/
/	Copyright:		2011 - 2012 (c) Broadcastweb.eu											/
/	License:		CC BY-NC-ND 3.0 ~ http://creativecommons.org/licenses/by-nc-nd/3.0/		/
/	File Version:	0003																	/
/																							/
/========================================== JooSam ========================================*/
// Joomla security
defined('_JEXEC') or die;
jimport('joomla.application.component.controlleradmin');

// Controller Servers view
class JooSamControllerServers extends JControllerAdmin{
	protected $text_prefix = 'Servers';

	public function switchto(){
		$srv = JRequest::getVar('id');
		if(!$srv){
			$srv = JRequest::getVar('cid');
		}
		$srv = (int)$srv[0];
		$db1 =& JFactory::getDBO();
		$query1 = "UPDATE ".$db1->nameQuote('#__joosam_server')." SET ".$db1->nameQuote('srv_active')." = ".$db1->quote(0)." WHERE ".$db1->nameQuote('srv_active')." = 1";
		$db1->setQuery($query1);
		$db1->query();
		$db2 =& JFactory::getDBO();
		$query2 = "UPDATE ".$db2->nameQuote('#__joosam_server')." SET ".$db2->nameQuote('srv_active')." = ".$db2->quote(1)." WHERE ".$db2->nameQuote('id')." = ".$db2->quote($srv).";";
		$db2->setQuery($query2);
		$db2->query();
		$db3 =& JFactory::getDBO();
		$query3 = "SELECT id, srv_name FROM ".$db3->nameQuote('#__joosam_server')." WHERE ".$db3->nameQuote('srv_active')." = 1";
		$db3->setQuery($query3);
		$row = $db3->loadAssoc();
		if($row){
			if($row['id'] == $srv){
				$names = $row['srv_name'];
				$this->setMessage(JText::_('COM_JOOSAM_SERVERS_CONT_SWITCHED_SERVER'). $names);
				$this->setRedirect(JRoute::_('index.php?option=com_joosam&view=servers', false));
			}
		}
		else{
			$this->setMessage(JText::_('COM_JOOSAM_SERVERS_CONT_SWITCHED_SERVER_ERROR'), 'warning');
			$this->setRedirect(JRoute::_('index.php?option=com_joosam&view=servers', false));
		}
	}

	public function testconnect(){
		$srv = JRequest::getVar('id');
		if(!$srv){
			$srv = JRequest::getVar('cid');
			$srv = $srv[0];
		}
		require_once JPATH_ADMINISTRATOR . DS . 'components' . DS . 'com_joosam' . DS . 'helpers' . DS . 'helper.samdb.php';

		$data = SamdbHelper::samdb('countSonglist', $srv);
		if($data['status'] == 'valid'){
			$this->setMessage(JText::_('COM_JOOSAM_SERVERS_CONT_TEST_CONNECT_SUCCESS'));
			$this->setRedirect(JRoute::_('index.php?option=com_joosam&view=servers', false));
		}
		else if($data['status'] == 'error'){
			$txt = JText::_('COM_JOOSAM_SERVERS_CONT_TEST_CONNECT_SUCCESS_ERROR').' - '.$data['data'];
			$this->setMessage($txt, 'warning');
			$this->setRedirect(JRoute::_('index.php?option=com_joosam&view=servers', false));
		}
	}

	public function setadmin(){
		$srv = JRequest::getVar('id');
		if(!$srv){
			$srv = JRequest::getVar('cid');
		}
		$srv = (int)$srv[0];
		$db1 =& JFactory::getDBO();
		$query1 = "UPDATE ".$db1->nameQuote('#__joosam_server')." SET ".$db1->nameQuote('set_admin')." = ".$db1->quote(0)." WHERE ".$db1->nameQuote('set_admin')." = 1";
		$db1->setQuery($query1);
		$db1->query();
		$db2 =& JFactory::getDBO();
		$query2 = "UPDATE ".$db2->nameQuote('#__joosam_server')." SET ".$db2->nameQuote('set_admin')." = ".$db2->quote(1)." WHERE ".$db2->nameQuote('id')." = ".$db2->quote($srv).";";
		$db2->setQuery($query2);
		$db2->query();
		$db3 =& JFactory::getDBO();
		$query3 = "SELECT id, srv_name FROM ".$db3->nameQuote('#__joosam_server')." WHERE ".$db3->nameQuote('set_admin')." = 1";
		$db3->setQuery($query3);
		$row = $db3->loadAssoc();
		if($row){
			if($row['id'] == $srv){
				$names = $row['srv_name'];
				$this->setMessage(JText::_('COM_JOOSAM_SERVERS_CONT_ADMINSWITCH'). $names);
				$this->setRedirect(JRoute::_('index.php?option=com_joosam&view=servers', false));
			}
		}
		else{
			$this->setMessage(JText::_('COM_JOOSAM_SERVERS_CONT_ADMINSWITCH_ERROR'), 'warning');
			$this->setRedirect(JRoute::_('index.php?option=com_joosam&view=servers', false));
		}
	}

	public function delete(){
		$data = JRequest::getVar('cid');
		$sqlid = (int)$data[0];
		if($sqlid == 1){
			$this->setMessage(JText::_('COM_JOOSAM_SERVERS_CONT_DEFAULT_DELETE_ERROR'), 'warning');
			$this->setRedirect(JRoute::_('index.php?option=com_joosam&view=servers', false));
		}
		else{ 
			$error = 0;
			$txt = '<table>';
			$delserver = $this->sqldelserver($sqlid);
			if($delserver){$txt .= '<tr><td>'.JText::_('Server is deleted').'</td></tr>';}
			else{$txt .= '<tr><td>'.JText::_('COM_JOOSAM_SERVERS_CONT_DELSRV_ERROR').$sqlid.'</td></tr>'; $error = 1;}
			$delevent = $this->sqldelevtreq($sqlid);
			if($delevent){$txt .= '<tr><td>'.JText::_('COM_JOOSAM_SERVERS_CONT_DELREQEVT').'</td></tr>';}
			else{$txt .= '<tr><td>'.JText::_('COM_JOOSAM_SERVERS_CONT_DELREQEVT_ERROR').$sqlid.'</td></tr>'; $error = 1;}
			$deltxt = $this->sqldeltxtcodes($sqlid);
			if($deltxt){$txt .= '<tr><td>'.JText::_('COM_JOOSAM_SERVERS_CONT_DELTXT').'</td></tr>';}
			else{$txt .= '<tr><td>'.JText::_('COM_JOOSAM_SERVERS_CONT_DELTXT_ERROR').$sqlid.'</td></tr>'; $error = 1;}
			$txt .= '</table>';
			if($error == 1){ $this->setMessage($txt, 'warning'); }
			else{$this->setMessage($txt);}
			$this->setRedirect(JRoute::_('index.php?option=com_joosam&view=servers', false));
		}
	}

	// Delete server
	protected function sqldelserver($sqlid){
		$db =& JFactory::getDBO();
		$query = " DELETE FROM ".$db->nameQuote('#__joosam_server')." WHERE ".$db->nameQuote('id')." = ".$db->quote($sqlid).";";
		$db->setQuery($query);
		$result = $db->query();
		if($result){return true;}
		else{return false;}
	}

	// Delete Events/Requests
	protected function sqldelevtreq($sqlid){
		$db =& JFactory::getDBO();
		$query = " DELETE FROM ".$db->nameQuote('#__joosam_evtreq')." WHERE ".$db->nameQuote('srv_id')." = ".$db->quote($sqlid).";";
		$db->setQuery($query);
		$result = $db->query();
		if($result){return true;}
		else{return false;}
	}

	// Delete Events/Requests
	protected function sqldeltxtcodes($sqlid){
		$db =& JFactory::getDBO();
		$query = " DELETE FROM ".$db->nameQuote('#__joosam_textstrings')." WHERE ".$db->nameQuote('srv_id')." = ".$db->quote($sqlid).";";
		$db->setQuery($query);
		$result = $db->query();
		if($result){return true;}
		else{return false;}
	}

	// Model
	public function &getModel($name = 'Server', $prefix = 'JooSamModel'){
		$model = parent::getModel($name, $prefix, array('ignore_request' => true));
		return $model;
	}
}